import { observable, action, autorun,toJS } from 'mobx';
import {WeaTableNew,WeaForm} from 'comsMobx'
const {TableStore} = WeaTableNew;
import { ListStore } from './listStore';
import {WeaTools} from "ecCom"
import {Modal,message} from "antd"
import * as Apis from "../apis/instock"
import {InstockHisStore} from './instockHisStore'
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

class InstockStore  extends ListStore{	
    title = "入库申请";
    topTab = [
		{ title: '填写入库单', key: "0" },
		{ title: '历史申请记录', key: "1" },
    ];//已切换到页面

    @observable searchParams ={
        tabkey : "0"
    }
    @observable visible = false;
    @observable showSearchAd = false;
    @observable rightMenu =[];
    conditiontype = {viewtype : "add"};
    @observable addForm = new WeaForm();
    @observable mainTableInfo = [];
    @observable subTableInfo ={};
    @observable initInstockData = [];

    //历史记录
    @observable condition = [];
    @observable instockInfoStore = new InstockHisStore();
    instockid = "";

    @observable verified = false;
    @observable hasRight = false; //权限
    
    @observable instockRemind;
    @observable batchBrowser;
    @observable isuse;
    @observable ismanager = false;

	@action
    initDatas=(params = {})=> {
		let newParams = {...this.conditiontype,...params};
        this.title = this.title;
        this.loading = true;
        this.addForm = new WeaForm();
		Apis.getInstockInfo(newParams).then(data=>{
            if(data.isright == "false"  && typeof(data.isright) !== "undefined"){
				this.hasRight = false;
				this.verified = true;
			}else{
                this.hasRight = true;
				this.verified = true;
                this.mainTableInfo = data.mainTableInfo;
                this.subTableInfo = data.subTableInfo;
                this.initInstockData = data.subTableInfo.columnDefine;
                this.batchBrowser = data.batchBrowser;
                this.isuse = data.isuse;
                //根据高级搜索条件初始化form
                this.addForm && !this.addForm.isFormInit && this.addForm.initFormFields(data.mainTableInfo);
            }
            this.loading = false;
            if(this.initInstockData[0]&&params.dataid){
                this.initInstockData[0].capitalid=params.dataid;
                this.initInstockData[0].capitalidspan=params.dataname;
                this.initInstockData[0].capitalspec=params.capitalspec;
                this.initInstockData[0].price=params.price;
            }
		});
        
    }
    
    @action 
    changeTab = (key)=>{
        this.searchParams={...this.searchParams,tabkey:key};
        if(key == "1"){
            this.doSearch();
            this.getHistoryCondition();
        }
    }
    //获取历史记录
    doSearch =(params={})=>{
        this.loading = true;
         //获取表单的参数值
		 const searchParamsAd = this.form.getFormParams();
         const newParams = {  ...searchParamsAd, ...params };
         for(let item in newParams){
            if(newParams[item] == ""){
                delete newParams[item]
            }
         }
        Apis.getInstockHislist(newParams).then(data=>{
            this.tableStore.getDatas(data.sessionkey,  params.current || 1);
            this.dataKey = data.sessionkey;
            this.ismanager = data.ismanager;
            this.loading = false;
        })
    }
    getHistoryCondition=(params={})=>{
        Apis.getInstockCondition(params).then(data=>{
            this.condition = data.condition;
			//根据高级搜索条件初始化form
			this.form && !this.form.isFormInit && this.form.initFormFields(data.condition);
        })
    }

    showColumn=()=>{
        this.tableStore.setColSetVisible(true);
		this.tableStore.tableColSet(true)
    }
    //入库单数据保存
    saveInstockDatas=(datas)=>{
        this.initInstockData = datas;
    }
    //提交入库单
    doInstock=()=>{
        this.visible = true;
        this.addForm.validateForm().then( f =>{
            let subfields = toJS(this.initInstockData);
            if(f.isValid&&this.instockRemind.showErrors()){
                let fields = this.addForm.getFormParams();
                //校验购置日期和入库日期
                if(fields['stockindate_gz']>fields['stockindate']){
                    message.error(getLabel('83582',"入库日期不能小于购置日期！"));
                    return false;
                }
                this.loading = true;
                let par = {instockid:""};
                par = {...par,...fields};
                par.subfields = JSON.stringify(subfields);
                Apis.doInstock(par).then(data=>{
                    if(data.result){
                        this.loading = false;
                        message.success(getLabel(751,"入库单")+getLabel(615,"提交")+getLabel(15242,"成功"));
                        this.initInstockData = [];
                        this.mainTableInfo = [];
                        this.subTableInfo = {};
                        this.initDatas();
                        
                        //入库验收左侧菜单统计
                        loadLeftMenuCount();
                    }else{
                        Modal.warning({
                            title: getLabel(15172, '系统提示'),
                            content: data.tip,
                        });
                        this.loading = false;
                    }
                    this.visible = false;
                 })
            }else{
                f.showErrors();
                this.instockRemind.showErrors();
                if(f.isValid&&subfields.length==0){
                    Modal.warning({
                        title: getLabel(15172, '系统提示'),
                        content: getLabel(33027, '请至少输入1条明细记录！')
                    });
                }
                this.visible = false;
            }
        })
    }

    //删除
    doDelete = (params={})=>{
        let _this = this;
        Apis.doDelete(params).then(data=>{
            if(data.flag){
                message.success(getLabel(91,"删除")+getLabel(15242,"成功"));
                _this.reLoad();
            }else{
                Modal.warning({
                    title: getLabel(15172, '系统提示'),
                    content: data.msg,
                }); 
            }
         })
    }

    check_form = ()=>{
		let checkstr = 'stockindate_gz,checkerid,stockindate'
        let subfields = toJS(this.initInstockData);
        let fields = this.addForm.getFormParams();
		let checkitem = checkstr.split(",");
        let bool = true;
        //先校验购置日期和入库日期 ，在校验必填
        if(fields['stockindate_gz']>fields['stockindate']){
            message.error(getLabel('83582',"入库日期不能小于购置日期！"));
            return false;
        }else{
            for(let i=0 ; i<checkitem.length ; i++) { 
                let checkid = checkitem[i];
                if(checkid){
                    if(!(fields[checkid])){
                        bool=false;
                    }
                }
            }
            subfields.map((item) =>{
                if(!(item.capitalid && item.capitalspec && item.price && item.capitalnum)){
                    bool=false;
                }
            })
            return bool;
        }
    }
   
	clearStatus=()=>{
        this.searchParams ={
            tabkey : "0"
        };
        this.showSearchAd = false;
        this.clearFormFields();
        this.subTableInfo ={};
        this.initInstockData = [];
    }

}

const cptInstockStore = new InstockStore();
export default cptInstockStore;